package com.ztc;

import com.common.Constant;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ztc on 15-12-30.下午4:09
 */
public class Spider {
    CloseableHttpClient client;
    //private static Spider spider=new Spider();
    private Spider(){
        this.client=HttpClients.createDefault();
    }

    /**
     * 单例模式.
     * 获得一个Spider实例。
     * @return
     */
    public static Spider getSpider(){
        return new Spider();
    }

    /**
     * 取得模拟登陆的浏览器
     * @return
     */
    public CloseableHttpClient getClient(){
        return this.client;
    }

    /**
     * 登陆模块.
     * 测试提交的账号是否能登陆成功.
     * @param xh　学号
     * @param sfzh　身份证号
     * @param kl　密码
     * @param yzm　验证码
     * @return　true为登陆成功，false为登陆失败
     */
    public boolean login(String xh,String sfzh,String kl,String yzm){
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("__VIEWSTATE", "/wEPDwUJODExMDE5NzY5ZGRgtUdRucUbXsT8g55XmVsTwV6PMw=="));
        params.add(new BasicNameValuePair("__VIEWSTATEGENERATOR", "6C0FF253"));
        params.add(new BasicNameValuePair("xh",xh));
        params.add(new BasicNameValuePair("sfzh",sfzh));
        params.add(new BasicNameValuePair("kl",kl));
        params.add(new BasicNameValuePair("yzm",yzm));
        try {
            UrlEncodedFormEntity uefe=new UrlEncodedFormEntity(params,"utf-8");
            HttpPost hp=new HttpPost(Constant.loginUrl);
            hp.setEntity(uefe);
            CloseableHttpResponse response=client.execute(hp);
            HttpEntity he=response.getEntity();
            InputStream is=he.getContent();
            //FileOutputStream fos=new FileOutputStream("login.html");
            byte[] buffer=new byte[1024];
            int count;
            StringBuilder str=new StringBuilder();
            while((count=is.read(buffer))!=-1){
                //fos.write(buffer,0,count);
                String bu=new String(buffer,0,count);
                str.append(bu);
            }
            is.close();
            //fos.close();
            //System.out.println(str.length());
            if(str.length()>200)
                return false;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     *
     * @return
     */
    public String getCjid(){
        HttpGet get=new HttpGet(Constant.cjidUrl);
        StringBuilder str=new StringBuilder();
        try {
            CloseableHttpResponse response=client.execute(get);
            HttpEntity he=response.getEntity();
            InputStream is=he.getContent();
            byte[] buffer=new byte[1024];
            int count;
            while((count=is.read(buffer))!=-1){
                String bu=new String(buffer,0,count);
                str.append(bu);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }
    public  String getName(){
        StringBuilder information=new StringBuilder();																//记录
        try {
            HttpGet get=new HttpGet(Constant.nameUrl);
            CloseableHttpResponse response= client.execute(get);
            HttpEntity he=response.getEntity();
            InputStream in=he.getContent();
            byte[] buffer=new byte[1024];
            int count;
            while((count=in.read(buffer))!=-1)
            {
                String inf=new String(buffer,0,count);
                information.append(inf);
            }
            in.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return information.toString();
    }
    public String getScore() {
        StringBuilder save=new StringBuilder();
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("start","0"));
        list.add(new BasicNameValuePair("pageSize","80"));
        UrlEncodedFormEntity entity;
        try {
            entity = new UrlEncodedFormEntity(list,"utf-8");
            HttpPost httpPost=new HttpPost(Constant.scoreUrl);
            httpPost.setEntity(entity);
            CloseableHttpResponse res=client.execute(httpPost);
            HttpEntity he=res.getEntity();
            InputStream in=he.getContent();
            //FileOutputStream out=new FileOutputStream("../webapps/ztc01/score.html");
            byte[] buffer=new byte[1024];
            int count;
            while((count=in.read(buffer))!=-1)
            {
                String sav=new String(buffer,0,count);
                save.append(sav);
            }
            in.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return save.toString();
    }
    public int closeClient() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
            return 1;
        }
        return 0;
    }
}
